<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>切换身份</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">
                    <form class="layui-form layui-form-pane" action=""
                          lay-filter="userInfo">
                        <input name="id" hidden>
                        <div class="layui-form-item">
                            <label class="layui-form-label">选择身份</label>
                            <div id="identityDiv"></div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">切换</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script th:inline="none">
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var form = layui.form;
    var element = layui.element;
    $(function () {
        CoreUtil.sendGet("/sys/user/identity", null, function (res) {
            if (res.data != null) {
                var _data = res.data;
                $("input[name=id]").val(_data[0].swryDm);
                var identityDivTmp = "";
                for (var i in _data) {
                    var _rysfmc = _data[i].rysfmc;
                    var _zsfbz = _data[i].zsfbz == "Y" ? "主身份"
                        : "副身份";
                    var _sfswjgDm = _data[i].sfswjgDm
                    var _swrysfDm = _data[i].swrysfDm;
                    var _rysfmc = _data[i].rysfmc;
                    var _value = _sfswjgDm + "_" + _swrysfDm
                        + "_" + _rysfmc;
                    if (_zsfbz == "主身份") {
                        identityDivTmp += "<div class='layui-input-block'>"
                            + "<input type='radio' name='identity' value='" + _value + "' title='" + _rysfmc + "-" + _zsfbz + "' checked>"
                            + "<input type='radio' name='identity1' value='" + _value + "' title='下次登录默认身份' checked>"
                            + "</div>";
                    } else {
                        identityDivTmp += "<div class='layui-input-block'>"
                            + "<input type='radio' name='identity' value='" + _value + "' title='" + _rysfmc + "-" + _zsfbz + "'>"

                            + "<input type='radio' name='identity1' value='" + _value + "' title='下次登录默认身份'>"
                            + "</div>";
                    }
                }

                $("#identityDiv").html(identityDivTmp);
                form.render(); //更新全部
            }
        });

        //监听提交
        form.on('submit(submit)', function (data) {
            var _id = data.field.id;
            console.log("id", _id)
            var _identityArr = data.field.identity.split("_");
            if (CoreUtil.isEmpty(_id)) {
                layer.msg("_id没有获取到！");
                return false;
            }

            if (CoreUtil.isEmpty(_identityArr)) {
                layer.msg("data.field.identity 没有获取到");
                return false;
            }

            var _identityArr1 = data.field.identity1.split("_");

            var map = {
                swrysfDm: _identityArr[1],
                sfswjgDm: _identityArr[0],
                rysfmc: _identityArr[2],
                swrysfDm1: _identityArr1[1],
                sfswjgDm1: _identityArr1[0],
                rysfmc1: _identityArr1[2]

            };

            CoreUtil.sendPut("/sys/user/switchidentity", map, function (res) {
                layer.msg(res.msg);
                if (res.code === 0) {
                    layer.msg(res.msg + ",5秒后自动刷新页面");
                    setTimeout(
                        function () {
                            // top.window.location.href = "/index/home?userId=" + res.data[0] + "&token=" + res.data[1] + "&roles=" + res.data[2];
                            // var rolesHeadle = res.data[2].replaceAll("[","").replaceAll("]","");
                            // 解决低版本浏览器问题 replaceAll
                            var rolesHeadle = res.data[2].replace(/\[|\]/g, "");
                            top.window.location.href = "/index/home?roles=" + rolesHeadle;
                            // top.window.location.reload();
                        }
                        , 2000);
                }

            });
            return false;
        });

    })
</script>
</html>